package com.huikeportal.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.AspnetUsers;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.CommonService;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.InterFaceCommon;

@Controller
public class FootfallGlobalAPI {
	
	@Autowired
	private FootfallGlobalService footfallGlobalService;
	
	@Autowired
	private InterFaceCommon interFaceCommon;
	
	@Autowired
	private CommonService commonService;
	

	/**取单个场所数据报表接口  */

	@RequestMapping(value = "/huike_getSiteDataAnalysis", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getGraphData(String userId, String siteId, String type,String span,String from,String to) throws Exception {
		
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteDataAnalysis");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		/**
		 * start Validate
		 */
		//1、是否为空验证、参数合法性验证
		StringBuffer reMassege = new StringBuffer();
		//userId验证
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		if(userId == null || "".equals(userId)){
			reMassege.append("userId为空错误;");
		}else{
			try {
				AspnetUsers aspnetUsers = commonService.getaspnetUsersMapper(userInfo);
				if(aspnetUsers == null){
					reMassege.append("userId不合法;");
				}
			} catch (Exception e) {
				reMassege.append("userId不合法;");
			}
		}
		//siteId验证
		if(siteId == null || "".equals(siteId)){
			reMassege.append("siteId为空错误;");
		}
		//type验证
		if(type == null || "".equals(type)){
			reMassege.append("type为空错误;");
		}else{
			HashMap<String, String>  indexInfo = footfallGlobalService.getIndexInfo();
			String typeName = indexInfo.get(type);
			if(typeName == null || "".equals(typeName)){
				reMassege.append("type不合法,"+type+";");
			}
		}
		//span验证
		if(span == null || "".equals(span)){
			reMassege.append("span为空错误;");
		}else{
			Pattern  pattern = Pattern.compile("^hour_(0|1|2)$|^day_(0|1|7|14|30)$|^week_(0|4|8|12)$|^month_(0|3|6|12)$|^year_(0|3|5)$|");
			Matcher  matcher = pattern.matcher(span);
			if(!matcher.matches()){
				reMassege.append("span不合法,"+span+";");
			}
			String[] Strings =span.split("_");
			//from验证
			if("0".equals(Strings[1])){
				if(from == null || "".equals(from)){
					reMassege.append("from为空错误,");
				}else{
					if(!commonService.isDateStr(from)){
						reMassege.append("from不合法,"+from+";");
					}
				}
				//to验证
				if(to == null || "".equals(to)){
					reMassege.append("to为空错误,");
				}else{
					if(!commonService.isDateStr(to)){
						reMassege.append("to不合法,"+to+";");
					}
				}
				if(commonService.isDateStr(to) && commonService.isDateStr(from)){
					SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
					Date fromDate = sdf.parse(from);
					Date toDate = sdf.parse(to);
					if(fromDate.after(toDate)){
						reMassege.append("from大于to错误,"+"from="+from+"to="+to+";");
					}
				}
				
			}
		}
		//如果验证有错误返回错误信息
		if(reMassege.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMassege.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		/**
		 * end  Validate
		 */
		
		/**
		 * start service
		 */
		try{
			String[] Strings =span.split("_");
			//日周月年
			String timeType_table=Strings[0];
			//类型，7、14、30天；4、8、12周；3、6、12月；3、5年
			String timeType=Strings[1];
			HashMap<String , Object> paraMapResult=new HashMap<String, Object>();
			
			//查询图表
			HashMap<String , Object> paraMap=new HashMap<String, Object>();
			
			paraMap.put("timeType", timeType);
			paraMap.put("timeType_table", timeType_table);//日周月年表
			paraMap.put("startDate", from);
			paraMap.put("endDate", to);
			paraMap.put("type", type);
			paraMap.put("siteId", siteId);
			paraMap.put("userInfo", userInfo);
			List<ReportGraphData> list= footfallGlobalService.getGraphData(paraMap);
			List<String> siteType = footfallGlobalService.getSiteTypes(siteId);
			/** 获取siteId 的场所类型中文名称*/
			String siteTypeName = "";
			if(siteType!=null&&!"".equals(siteType)){
				String siteType1 = siteType.get(0);
				siteTypeName = footfallGlobalService.getSiteTypeName(Integer.parseInt(siteType1));
			}
			paraMapResult.put("siteTypeName", siteTypeName);
			
			//插入数据日期类型，日周年月
			paraMapResult.put("timeType_table",timeType_table);
			//插入折线数据
			paraMapResult.put("result", list);
			String siteNames=footfallGlobalService.getSiteNames(siteId);//前台导航栏地址 广场-场所
			paraMapResult.put("siteN", siteNames);
			String sitename=footfallGlobalService.getSiteName(siteId);//siteId的中文名称
			paraMapResult.put("siteName", sitename);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(paraMapResult);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		/**
		 * end service
		 */
		
	}
	
	/**取单个场所下面所有子场所数据，（我的客流-排行榜）  */

	@RequestMapping(value = "/huike_getGlobalRanking", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getGraphData1(String userId, String siteId, String type,String span,String from,String to,String orderBy) throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteDataAnalysis");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		
		/**
		 * start Validate
		 */
		//1、是否为空验证、参数合法性验证
		StringBuffer reMassege = new StringBuffer();
		//userId验证
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		if(userId == null || "".equals(userId)){
			reMassege.append("userId为空错误;");
		}else{
			try {
				AspnetUsers aspnetUsers = commonService.getaspnetUsersMapper(userInfo);
				if(aspnetUsers == null){
					reMassege.append("userId不合法;");
				}
			} catch (Exception e) {
				reMassege.append("userId不合法;");
			}
		}
		//siteId验证
		if(siteId == null || "".equals(siteId)){
			reMassege.append("siteId为空错误;");
		}
		//type验证
		if(type == null || "".equals(type)){
			reMassege.append("type为空错误;");
		}else{
			HashMap<String, String>  indexInfo = footfallGlobalService.getIndexInfo();
			String typeName = indexInfo.get(type);
			if(typeName == null || "".equals(typeName)){
				reMassege.append("type不合法,"+type+";");
			}
		}
		//orderBy验证
		if(orderBy == null || "".equals(orderBy)){
			reMassege.append("orderBy为空错误;");
		}else{
			Pattern  pattern = Pattern.compile("^0$|^1$|");
			Matcher  matcher = pattern.matcher(orderBy);
			if(!matcher.matches()){
				reMassege.append("orderBy不合法,"+orderBy+";");
			}
			
		}
		//span验证
		if(span == null || "".equals(span)){
			reMassege.append("span为空错误;");
		}else{
			Pattern  pattern = Pattern.compile("^hour_(0|1|2)$|^day_(0|1|7|14|30)$|^week_(0|4|8|12)$|^month_(0|3|6|12)$|^year_(0|3|5)$|");
			Matcher  matcher = pattern.matcher(span);
			if(!matcher.matches()){
				reMassege.append("span不合法,"+span+";");
			}
			String[] Strings =span.split("_");
			//from验证
			if("0".equals(Strings[1])){
				if(from == null || "".equals(from)){
					reMassege.append("from为空错误,");
				}else{
					if(!commonService.isDateStr(from)){
						reMassege.append("from不合法,"+from+";");
					}
				}
				//to验证
				if(to == null || "".equals(to)){
					reMassege.append("to为空错误,");
				}else{
					if(!commonService.isDateStr(to)){
						reMassege.append("to不合法,"+to+";");
					}
				}
				if(commonService.isDateStr(to) && commonService.isDateStr(from)){
					SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
					Date fromDate = sdf.parse(from);
					Date toDate = sdf.parse(to);
					if(fromDate.after(toDate)){
						reMassege.append("from大于to错误,"+"from="+from+"to="+to+";");
					}
				}
				
			}
		}
		//如果验证有错误返回错误信息
		if(reMassege.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMassege.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		/**
		 * end  Validate
		 */
		
		/**
		 * start service
		 */
		try{
			String[] Strings = span.split("_");
			//日周月年
			String timeType_table=Strings[0];
			//类型，7、14、30天；4、8、12周；3、6、12月；3、5年
			String timeType=Strings[1];
			
			HashMap<String , Object> paraMap = new HashMap<String, Object>();
			paraMap.put("startDate", from);
			paraMap.put("endDate", to);
			paraMap.put("siteId", siteId);
			paraMap.put("type", type);
			paraMap.put("span", span);
			paraMap.put("timeType_table", timeType_table);
			paraMap.put("timeType", timeType);
			paraMap.put("orderBy", orderBy);
			paraMap.put("userInfo", userInfo);
			List<HashMap<String,Object>> stringRank=footfallGlobalService.getRanking(paraMap);
			paraMap.put("result", stringRank);
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(paraMap);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		/**
		 * end service
		 */
		
	}
	
	/**求日报表场所指定指标的最大值 */

	@RequestMapping(value = "/huike_getSiteMaxDataOfIndex", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getSiteMaxDataOfIndex(String siteId, String type,String dateType) throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteMaxDataOfIndex");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象

		MsgResponse msgResponse = new MsgResponse();
		
		/**
		 * start Validate
		 */
		//1、是否为空验证、参数合法性验证
		StringBuffer reMassege = new StringBuffer();
		//siteId验证
		if(siteId == null || "".equals(siteId)){
			reMassege.append("siteId为空错误;");
		}
		//type验证
		if(type == null || "".equals(type)){
			reMassege.append("type为空错误;");
		}else{
			String[] types = type.split(",");
			for(String ty : types){
				HashMap<String, String>  indexInfo = footfallGlobalService.getIndexInfo();
				String typeName = indexInfo.get(ty);
				if(typeName == null || "".equals(typeName)){
					reMassege.append("type不合法,"+ty+";");
				}
			}
		}
		//span验证
		//如果验证有错误返回错误信息
		if(reMassege.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMassege.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		/**
		 * end  Validate
		 */
		
		/**
		 * start service
		 */
		try{
			//日周月年
			HashMap<String , Object> paraMapResult=new HashMap<String, Object>();
			
			//查询图表
			HashMap<String , Object> paraMap = new HashMap<String, Object>();
			
			paraMap.put("type", type);
			paraMap.put("siteId", siteId);
			paraMap.put("dateType", dateType);
			ReportGraphData reportGraphData = footfallGlobalService.getSiteMaxDataOfIndex(paraMap);
			paraMapResult.put("result", reportGraphData);
			
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(paraMapResult);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		/**
		 * end service
		 */
		
	}
	
	
	/**、获取场所信息 */

	@RequestMapping(value = "/huike_getSiteAllInfo", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getSiteAllInfo(String siteId) throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteAllInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象

		MsgResponse msgResponse = new MsgResponse();
		
		/**
		 * start Validate
		 */
		//1、是否为空验证、参数合法性验证
		StringBuffer reMassege = new StringBuffer();
		//siteId验证
		if(siteId == null || "".equals(siteId)){
			reMassege.append("siteId为空错误;");
		}else{
			String retSiteInfo	= footfallGlobalService.getSiteAllInfoById(siteId);
			if(retSiteInfo.length() > 0){
				reMassege.append("siteId不合法;"+retSiteInfo+";");
			}
		}
		//如果验证有错误返回错误信息
		if(reMassege.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMassege.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		/**
		 * end  Validate
		 */
		
		/**
		 * start service
		 */
		try{
			//日周月年
			HashMap<String , Object> paraMapResult=new HashMap<String, Object>();
			//查询图表
			SiteAllInfo siteAllInfo = footfallGlobalService.getSiteAllInfo(siteId);
			paraMapResult.put("result", siteAllInfo);
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(paraMapResult);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		/**
		 * end service
		 */
		
	}
	
	
	/**、根据广场ID获取集团场所Id信息 */

	@RequestMapping(value = "/huike_JituanIdByguangchangId", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getJituanIdByguangchangId(String siteId) throws Exception {
		
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_JituanIdByguangchangId");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象

		MsgResponse msgResponse = new MsgResponse();
		
		/**
		 * start Validate
		 */
		//1、是否为空验证、参数合法性验证
		StringBuffer reMassege = new StringBuffer();
		//siteId验证
		if(siteId == null || "".equals(siteId)){
			reMassege.append("siteId为空错误;");
		}else{
			String retSiteInfo	= footfallGlobalService.getSiteAllInfoById(siteId);
			if(retSiteInfo.length() > 0){
				reMassege.append("siteId不合法;"+retSiteInfo+";");
			}
		}
		//如果验证有错误返回错误信息
		if(reMassege.length()>0){
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			msgResponse.setObject(reMassege.toString());
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		
		/**
		 * end  Validate
		 */
		
		/**
		 * start service
		 */
		try{
			//日周月年
			HashMap<String , Object> paraMapResult=new HashMap<String, Object>();
			//查询图表
			String jituanSiteId = footfallGlobalService.getJituanIdByguangchangId(siteId);
			paraMapResult.put("result", jituanSiteId);
			/**
			 *  end 取数据
			 */
		 	msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			msgResponse.setObject(paraMapResult);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		/**
		 * end service
		 */
		
	}
	
	
}
